package com.example.controller.xitongshouye;

import com.example.pojo.User;
import com.example.service.xitongshouye.ZhanghuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName: ***
 * @Description: ***
 * @author: ***
 * @date: 2022/11/25 19:53
 */
@Controller
@RequestMapping("/zhanghu")
public class ZhanghuController {
    @Autowired
    private ZhanghuService zhanghuService;

    @Value("${file.path}")
    private String path;

    /**
    * @Author: zdp
    * @Description: 查询用户信息
    * @DateTime: 19:54 2022/11/25
    * @Params:
    * @Return
    */
    @ResponseBody
    @PostMapping("/user")
    @CrossOrigin
    public User getuser(String account){
        return zhanghuService.getuser(account);
    }

    /**
    * @Author: zdp
    * @Description: 修改账号密码
    * @DateTime: 20:35 2022/11/25
    * @Params:
    * @Return
    */
    @ResponseBody
    @PostMapping("/uppassword")
    @CrossOrigin
    public int uppassword(String password,String account){

        return zhanghuService.uppassword(password,account);
    }
    /**
     * @Author: zdp
     * @Description: 修改头像路径
     * @DateTime: 11:50 2022/11/28
     * @Params:
     * @Return
     */
    @ResponseBody
    @CrossOrigin
    @RequestMapping("/uptouxiang")
    public Map<String, Object> upload(MultipartFile file,String account){
        String originalFilename = file.getOriginalFilename();//当前文件名
        int i = originalFilename.indexOf(".");//获取“.”第一次出现的索引
        String substring = originalFilename.substring(i);
        UUID uuid = UUID.randomUUID();//生成随机的字符串，以免重复
        File file1 = new File(path + uuid + substring);
        zhanghuService.uptouxiang("http://localhost:8081/image/"+uuid + substring,account);
        try {
            //上传保存到file1中
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("files",uuid+substring);
        return map;
    }
}
